package com.haoqizhe.kernel.activemq.listener;

import com.haoqizhe.kernel.activemq.connection.ActiveMQConnectionFactoryConfiguration;
import org.apache.activemq.ActiveMQSession;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.context.annotation.*;
import org.springframework.jms.config.DefaultJmsListenerContainerFactory;
import org.springframework.jms.connection.CachingConnectionFactory;

import javax.jms.Session;

/**
 * @author  haoqizhe.li on 2017/11/3.
 */
@Configuration
@DependsOn(value = "cachingReceiveConnectionFactory")
@Import({ActiveMQConnectionFactoryConfiguration.class,ListenerHandler.class})
public class TopicListenerContainerConfig {

    @Autowired
    @Qualifier(value = "cachingReceiveConnectionFactory")
    private CachingConnectionFactory cachingReceiveConnectionFactory;

    /**
     * 配置topic监听器,事务提交确认
     * @param listenerHandler
     * @return  JmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/12 21:03
     **/
    @Bean(name="containerFactory4TopicSession")
    public DefaultJmsListenerContainerFactory containerFactory4TopicSession(ListenerHandler listenerHandler){

        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);


        factory.setErrorHandler(listenerHandler);
        /**
         * 开启pub/sub发布订阅模式
         */
        factory.setPubSubDomain(true);
        /**
         * 开启持久化订阅者
         */
        factory.setSubscriptionDurable(true);
        /**
         * 开启事务支持，为true时，sessionAcknowledgeMode 无效，默认为 Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionTransacted(true);
        /**
         * Session.AUTO_ACKNOWLEDGE; 1  自动确认
         * Session.CLIENT_ACKNOWLEDGE; 2  客户端确认
         * Session.DUPS_OK_ACKNOWLEDGE; 3  批量确认
         * Session.SESSION_TRANSACTED; 0  事务提交确认
         */
        factory.setSessionAcknowledgeMode(Session.SESSION_TRANSACTED);

        return factory;

    }
    /**
     * topic监听器，自动确认
     * @return DefaultJmsListenerContainerFactory
     */
    @Bean(name="containerFactory4TopicAuto")
    public DefaultJmsListenerContainerFactory containerFactory4TopicAuto(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);
        /**
         * 开启pub/sub发布订阅模式
         */
        factory.setPubSubDomain(true);
        /**
         * 开启持久化订阅者
         */
        factory.setSubscriptionDurable(true);

        /**
         * 自动提交确认
         */
        factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);

        return factory;
    }

    /**
     * topic监听器，自动确认 ,非持久化订阅者
     * @return DefaultJmsListenerContainerFactory
     */
    @Bean(name="containerFactory4TopicAutoNoDB")
    public DefaultJmsListenerContainerFactory containerFactory4TopicAutoNoDB(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);
        /**
         * 开启pub/sub发布订阅模式
         */
        factory.setPubSubDomain(true);

        /**
         * 自动提交确认
         */
        factory.setSessionAcknowledgeMode(Session.AUTO_ACKNOWLEDGE);

        return factory;
    }

    /**
     * topic监听器，客户端手动确认，需在接收方法中手动message.acknowledgeMode()确认
     * @return org.springframework.jms.config.DefaultJmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/27 16:08
     **/
    @Bean(name="containerFactory4TopicIndividual")
    public DefaultJmsListenerContainerFactory containerFactory4TopicClient(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);
        /**
         * 开启pub/sub发布订阅模式
         */
        factory.setPubSubDomain(true);
        /**
         * 开启持久化订阅者
         */
        factory.setSubscriptionDurable(true);

        /**
         * 客户端手动确认
         */
        factory.setSessionAcknowledgeMode(ActiveMQSession.INDIVIDUAL_ACKNOWLEDGE);

        factory.setSessionTransacted(true);

        return factory;
    }

    /**
     * topic监听器，批量确认
     * @return org.springframework.jms.config.DefaultJmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/27 16:11
     **/
    @Bean(name="containerFactory4TopicDups")
    public DefaultJmsListenerContainerFactory containerFactory4TopicClientDups(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);
        /**
         * 开启pub/sub发布订阅模式
         */
        factory.setPubSubDomain(true);
        /**
         * 开启持久化订阅者
         */
        factory.setSubscriptionDurable(true);

        /**
         *  自动批量确认
         */
        factory.setSessionAcknowledgeMode(Session.DUPS_OK_ACKNOWLEDGE);

        return factory;
    }

    /**
     * topic监听器，批量确认，非持久化订阅者
     * @return org.springframework.jms.config.DefaultJmsListenerContainerFactory
     * @author haoqizhe.li
     * @date 2017/6/27 16:11
     **/
    @Bean(name="containerFactory4TopicDupsNoDB")
    public DefaultJmsListenerContainerFactory containerFactory4TopicClientDupsNoDB(){
        DefaultJmsListenerContainerFactory factory = new DefaultJmsListenerContainerFactory();
        factory.setConnectionFactory(cachingReceiveConnectionFactory);
        /**
         * 开启pub/sub发布订阅模式
         */
        factory.setPubSubDomain(true);
        /**
         *  自动批量确认
         */
        factory.setSessionAcknowledgeMode(Session.DUPS_OK_ACKNOWLEDGE);

        return factory;
    }
}
